<link rel="stylesheet" href="<%- Virtual_Path.Static %>jquery.plugin/pagination/pagination.css">
<script src="<%- Virtual_Path.Static %>jquery.plugin/pagination/pagination.js"></script>
<script src="<%- Virtual_Path.Static %>jquery.plugin/birthday/birthday.js"></script>
<div class="anchor-tab">
    <ul class="tab">
        <li><a href="/member/anchor">申请提现</a></li>
        <li><a href="/member/anchor/salary">基本工资</a></li>
        <li class="on"><a href="/member/anchor/live_data">直播数据</a></li>
        <li><a href="/member/anchor/gift_data">礼物数据</a></li>
    </ul>
</div>
<div class="anchor-cont">
    <div class="tabs-cont">
        <div class="hander-func">
            <p class="fl">查询时间：
                <select name="Birthday_Year" id="sel_year" rel=""></select>
                年
                <select name="Birthday_Month" id="sel_month" rel=""></select>
                月
            </p>
            <a href="javascript:;" class="c-button fl" id="J_Anchor_LiveData">查询</a>
            <p class="fr">
                累计开播时长：<em class="c-orange" id="J_Live_Duration_Total"></em>
            </p>
        </div>
        <div class="tableinfo">
            <table class="tabinfo">
                <thead>
                <tr>
                    <th>日期</th>
                    <th>直播累计时长</th>
                    <th>有效时长</th>
                </tr>
                </thead>
                <tbody id="J_OrderList">
                <tr>
                    <td colspan="5">加载中...</td>
                </tr>
                </tbody>
            </table>
        </div>
        <div id="AjaxPagination" class="pagination"></div>
    </div>
</div>
<script>
    $(function () {
        var _ThisPage = {
            PageParm: {
                P_Get: true,
                Loading: false, //加载中
                Total: null,       //总条数
                MaxCode: "",    //MaxCode
                Start: 0,       //开始
                Length: 10      //页长度
            }
        };
        var Create_Date = '';
        //时间搜索插件
        $.ms_DatePicker({
            YearSelector: ".sel_year",
            MonthSelector: ".sel_month",
        });
        $.ms_DatePicker();
        if (_PageData.Live_Duration_Total > 0) {
            _PageData.Live_Duration_Total = (Math.floor(_PageData.Live_Duration_Total / 60) + "小时" + (_PageData.Live_Duration_Total % 60) + "分" )
        } else {
            _PageData.Live_Duration_Total = 0;
        }
        $('#J_Live_Duration_Total').text(_PageData.Live_Duration_Total);
        //搜索
        $('#J_Anchor_LiveData').on('click', function () {
            _ThisPage = {
                PageParm: {
                    P_Get: true,
                    Loading: false, //加载中
                    Total: null,       //总条数
                    MaxCode: "",    //MaxCode
                    Start: 0,       //开始
                    Length: 10      //页长度
                }
            };
            var param = {};
            param.year = $.trim($('#sel_year').val());
            param.month = $.trim($('#sel_month').val());
            if (param.year == 0) {
                $.KOC.Tips.Open("Error", '请选择年份！', {Timeout: 2000});
                return;
            }
            if (param.month == 0) {
                $.KOC.Tips.Open("Error", '请选择月份！', {Timeout: 2000});
                return;
            }
            Create_Date = param.year + '-' + param.month;
            AnchorData();
        });
        if (Create_Date == '') {
            AnchorData();
        }
        function AnchorData() {
            $.ajax({
                url: '/ajax/user.order-live.pageuser',
                type: 'post',
                dataType: 'json',
                data: {
                    P_Get: _ThisPage.PageParm.P_Get,
                    P_Total: _ThisPage.PageParm.Total,
                    P_MaxCode: _ThisPage.PageParm.MaxCode,
                    iDisplayStart: _ThisPage.PageParm.Start,
                    iDisplayLength: _ThisPage.PageParm.Length,
                    Sort_Col: "Report_Day",
                    Sort_Dir: "DESC",
                    Report_Day: Create_Date
                },
                success: function (data) {
                    _ThisPage.PageParm.Loading = false;
                    _ThisPage.PageParm.P_Get = false;
                    if (data.flag !== 0) {
                        $.KOC.Tips.Open("Error", data.msg + "(" + data.flag + ")", {Timeout: 3000});
                        return;
                    }
                    _ThisPage.PageParm.Total = data.iTotalRecords;
                    if (data.iTotalRecords > 0 || (!Create_Date || (Create_Date && moment(new Date(Create_Date)).format('YYYY-MM') == moment(new Date()).format('YYYY-MM')))) {
                        $("#AjaxPagination").empty().pagination(data.iTotalRecords, {
                            num_edge_entries: 2, //边缘页数
                            num_display_entries: 5, //主体页数
                            callback: PageCallback,
                            items_per_page: _ThisPage.PageParm.Length, //每页显示1项
                            prev_text: "前一页",
                            next_text: "后一页"
                        });
                    } else {
                        var _html = "";
                        _html += RenderPage(null, data.iTotalRecords);
                        $("#AjaxPagination").empty();
                        $('#J_OrderList').empty().html(_html);
                    }
                }
            })
        }

        function PageCallback(id) {
            _ThisPage.PageParm.Start = id * _ThisPage.PageParm.Length;
            InitTable(id);
        }

        function InitTable(index) {
            $.ajax({
                url: '/ajax/user.order-live.pageuser',
                type: 'post',
                dataType: 'json',
                data: {
                    P_Get: _ThisPage.PageParm.P_Get,
                    P_Total: _ThisPage.PageParm.Total,
                    P_MaxCode: _ThisPage.PageParm.MaxCode,
                    iDisplayStart: _ThisPage.PageParm.Start,
                    iDisplayLength: _ThisPage.PageParm.Length,
                    Sort_Col: "Report_Day",
                    Sort_Dir: "DESC",
                    Report_Day: Create_Date
                },
                success: function (data) {
                    _ThisPage.PageParm.Loading = false;
                    _ThisPage.PageParm.P_Get = false;
                    if (data.flag !== 0) {
                        $.KOC.Tips.Open("Error", data.msg + "(" + data.flag + ")", {Timeout: 3000});
                        return;
                    }
                    _ThisPage.PageParm.Total = data.iTotalRecords;
                    _ThisPage.PageParm.MaxCode = data.sMaxCode;
                    _ThisPage.PageParm.Start += _ThisPage.PageParm.Length;
                    var _html = '';
                    if (_PageData.Duration) {
                        if (!Create_Date || (Create_Date && moment(new Date(Create_Date)).format('YYYY-MM') == moment(new Date()).format('YYYY-MM'))) {
                            var Duration = (_PageData.Duration > 0) ? Math.floor(_PageData.Duration / 60) + "小时" + (_PageData.Duration % 60) + "分" : 0;
                            var Live_Duration_Efftive = (_PageData.Live_Duration_Efftive > 0) ? Math.floor(_PageData.Live_Duration_Efftive / 60) + "小时" + (_PageData.Live_Duration_Efftive % 60) + "分" : 0;
                            var Datetime = _PageData.Create_Date ? moment(_PageData.Create_Date).format('YYYY-MM-DD') : '0000-00-00'
                            _html += '<tr>' +
                                    '<td>' + Datetime +
                                    '</td>' +
                                    '<td>' + Duration +
                                    '</td>' +
                                    '<td>' + Live_Duration_Efftive +
                                    '</td>' +
                                    '</tr>';
                        }

                    }
                    if (_ThisPage.PageParm.Total > 0) {
                        _.forEach(data.aaData, function (ThisValue) {
                            _html += RenderPage(ThisValue);
                        });
                    }
                    $('#J_OrderList').empty().html(_html);
                }
            });
        }

        function RenderPage(ThisValue, len) {
            var _html = '';
            if (len <= 0) {
                _html += '<tr>' +
                        '<td colspan="5">没有直播记录！' +
                        '</td>' +
                        '</tr>';
            } else {
                ThisValue.Live_Duration = ThisValue.Live_Duration > 0 ? (Math.floor(ThisValue.Live_Duration / 60) + "小时" + (ThisValue.Live_Duration % 60) + "分" ) : 0;
                ThisValue.Live_Duration_Efftive = ThisValue.Live_Duration_Efftive > 0 ? (Math.floor(ThisValue.Live_Duration_Efftive / 60) + "小时" + (ThisValue.Live_Duration_Efftive % 60) + "分" ) : 0;

                _html += '<tr>' +
                        '<td>' + moment(ThisValue.Report_Day).format('YYYY-MM-DD') +
                        '</td>' +
                        '<td>' + ThisValue.Live_Duration +
                        '</td>' +
                        '<td>' + ThisValue.Live_Duration_Efftive +
                        '</td>' +
                        '</tr>';
            }
            return _html;
        }
    });
</script>